<?php
/**
 * Customer Controller
 * Administration of the ecommerce customers.
 */
class Admin_CustomerController extends ImpSoft_Controller_Action {

	public function init() {
	
		parent::init();
		$this->custTbl = new Customer;
		$this->statTbl = new State;
		$this->userTbl = new User;
		$this->addrTbl = New Address;
	
	}
	
	public function listAction() {
	
		$query = $this->getRequest()->getQuery();
		$page = $this->getRequest()->getParam("page");
		$order = (empty($query['order'])) ? null : $query['order'];
		$dir = (empty($query['dir'])) ? null : $query['dir'];
		$this->view->dir = (strtolower($dir) == "asc") ? "desc" : "asc";
		$select = $this->custTbl->getListForAdmin($order, $dir);
		$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select));
		// @todo Eventually this should come from db settings
		$paginator->setItemCountPerPage(100);
		$paginator->setCurrentPageNumber($page);
		$this->view->customers = $paginator;
	
	}
	/**
	 * @todo All the rules and filters and stuff in this are pretty lame. I would like to find
	 * a more elegant solution for all this but for now this works fine.
	 */
	public function addAction() {
	
		$user = $this->userTbl->createRow();
		$customer = $this->custTbl->createRow();
		// @todo I need a more elegant solution for mapping form elemnent names to model columns...
		$billing = $this->addrTbl->createRow();
		$shipping = $this->addrTbl->createRow();
		// build filters from tables
		$user_filters = with(new Q_Filter_Set())
			->add($this->userTbl->getFilters());
		$customer_filters = with(new Q_Filter_Set())
			->add($this->custTbl->getFilters());
		$billing_filters = with(new Q_Filter_Set())
			->add($this->addrTbl->getFilters());
		$shipping_filters = with(new Q_Filter_Set())
			->add($this->addrTbl->getFilters());
		// build rules from tables
		$user_rules = with(new Q_Rule_Set())
			->add($this->userTbl->getRules());
		$customer_rules = with(new Q_Rule_Set())
			->add($this->custTbl->getRules());
		$billing_rules = with(new Q_Rule_Set())
			->add($this->addrTbl->getRules());
		$shipping_rules = with(new Q_Rule_Set())
			->add($this->addrTbl->getRules());
		// initialize the errors array
		list($user_errors, $customer_errors, $billing_errors, $shipping_errors) = array(array(),array(),array(),array());
		// if data has been posted, handle it
		if ($this->isPost()) {
			$post = $this->getPost();
			// filter post data
			$user_post = $user_filters->filter($post['user']);
			$customer_post = $customer_filters->filter($post['customer']);
			$billing_post = $billing_filters->filter($post['billing']);
			$shipping_post = $shipping_filters->filter($post['shipping']);

			// @todo This might need some help.. and we need to create a user and some addresses here too
			$user->setFromArray($user_post);
			$user->user_role = 'customer';
			$customer->setFromArray($customer_post);
			// @todo Need an elegant way to map column names to model names
			$billing->setFromArray($billing_post);
			$shipping->setFromArray($shipping_post);

			// validate filtered data
			$user_valid = $user_rules->validate($user_post);
			$customer_valid = $customer_rules->validate($customer_post);
			$billing_valid = $billing_rules->validate($billing_post);
			$shipping_valid = $shipping_rules->validate($shipping_post);
			if ($user_valid && $customer_valid && $billing_valid && $shipping_valid) {
				// begin database transaction
				$this->db->beginTransaction();
				try {
					$user = $this->userTbl->registerUser($user_post);
					$customer->user_id = $user->user_id;
					$customer->save();
					$billing->user_id = $user->user_id;
					$billing->type = 'customer_billing';
					$billing->save();
					$shipping->user_id = $user->user_id;
					$shipping->type = 'customer_shipping';
					$shipping->save();
					// commit any queries
					$this->db->commit();
					$this->flash("Product was created successfully", "notice");
					$this->goToRoute("admin_customer_add");
					return;
				} catch (Zend_Db_Exception $e) {
					// if any data couldn't be saved, roll back everything
					$this->db->rollback();
					$this->flash("Product could not be saved, sorry!", "error");
					pre($e);
					//$this->goToRoute("admin_customer_add");
					return;
				}
			} else {
				$user_errors = $user_rules->getErrors();
				$customer_errors = $customer_rules->getErrors();
				$billing_errors = $billing_rules->getErrors();
				$shipping_errors = $shipping_rules->getErrors();
			}
		}
		$this->view->user_errors = $user_errors;
		$this->view->customer_errors = $customer_errors;
		$this->view->billing_errors = $billing_errors;
		$this->view->shipping_errors = $shipping_errors;
		// $this->view->required = $rules->getRequired();
		$this->view->customer = $customer->toArray();
		$this->view->user = $user->toArray();
		$this->view->billing = $billing->toArray();
		$this->view->shipping = $shipping->toArray();
		$this->view->allstates = $this->statTbl->getStates();
	
	}
	
	public function editAction() {
	
		
	
	}

}